package com.example.arithmeticleetcode.learnArithmetic.quque;

import java.util.Stack;

/**
 * @program: arithmetic-leetcode
 * @description: 使用栈实现队列
 * @author: FangZhen
 * @create: 2020-08-20 16:36
 **/
public class QuqueByStack<E> {
    private Stack<E> stack = new Stack<>();

    public int size() {
        return stack.size();
    }

    public boolean isEmpty() {
        return stack.isEmpty();
    }

    public void enQuque(E e) {
        stack.push(e);
    }

    public E deQuque() {
        Stack<E> curStack = new Stack<>();
        while (!stack.isEmpty()) {
            curStack.push(stack.pop());
        }
        E result = curStack.pop();
        stack.clear();
        while (!curStack.isEmpty()) {
            stack.push(curStack.pop());
        }
        return result;
    }

    public E front() {
        Stack<E> curStack = new Stack<>();
        while (!stack.isEmpty()) {
            curStack.push(stack.pop());
        }
        E result = curStack.peek();
        stack.clear();
        while (!curStack.isEmpty()) {
            stack.push(curStack.pop());
        }
        return result;
    }

}
